Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
MongoDB javascript `"$function"` not returning expected value in mongoplayground.net

N.B.: This is the first javascript function I've written. My question is inspired by another question inquiring about recursive descent within a MongoDB document.

I tried the function on jsfiddle.net and it works, but when I try it on mongoplayground.net, it doesn't work.

Input collection:

[
  {
    "_id": 0,
    "text": "Node1",
    "children": [
      {
        "text": "Node2",
        "children": []
      },
      {
        "text": "Node3",
        "children": [
          {
            "text": "Node4",
            "children": [
              {
                "text": "Node5",
                "children": []
              },
              {
                "text": "Node6",
                "children": []
              }
            ]
          },
          {
            "text": "Node7",
            "children": []
          }
        ]
      }
    ]
  }
]

mongoplayground.net aggregation pipeline:

db.collection.aggregate([
  {
    "$set": {
      "texts": {
        "$function": {
          "body": "function(t, n) {function drill(t, n) {if (n.length > 0) {for (let elem of n) {t.push(elem.text); drill(t, elem.children)}} return t};drill(t,n)}",
          "args": [
            [
              "$text"
            ],
            "$children"
          ],
          "lang": "js"
        }
      }
    }
  }
])

jsfiddle.net outputs the desired result, ["Node1", "Node2", "Node3", "Node4", "Node5", "Node6", "Node7"], but mongoplayground.net sets "texts" to null.

Is there a way to fix the MongoDB "$function" or is this a limitation of mongoplayground.net or MongoDB?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

How about just removing the external function to get your desired results:

    db.collection.aggregate([
  {
    "$addFields": {
      "texts": {
        "$function": {
          "body": "function drill(t, n) {if (n.length > 0) {for (let elem of n) {t.push(elem.text); drill(t, elem.children)}} return t};",
          "args": [
            [
              "$text"
            ],
            "$children"
          ],
          "lang": "js"
        }
      }
    }
  }
])

You can see it works here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda